//
//  RouUIImage.swift
//  RouSwift
//
//  Created by leecong on 2017/8/18.
//  Copyright © 2017年 leecong. All rights reserved.
//

import UIKit

extension UIImage {
    
    public func trans(_ toSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        _ = UIGraphicsGetCurrentContext()
        self.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image ?? UIImage()
    }
    
    public convenience init(color:UIColor,size:CGSize){
        UIGraphicsBeginImageContextWithOptions(size, false, 1)
        color.setFill()
        UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        guard let img = UIGraphicsGetImageFromCurrentImageContext() else {
            self.init()
            return
        }
        self.init(cgImage: img.cgImage!)
    }
    
}
